package person.drf.shop.coupon.service.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import person.drf.common.to.MemberPrice;
import person.drf.common.to.SkuReductionTo;
import person.drf.common.utils.PageUtils;
import person.drf.common.utils.Query;

import person.drf.shop.coupon.dao.SkuFullReductionDao;
import person.drf.shop.coupon.entity.MemberPriceEntity;
import person.drf.shop.coupon.entity.SkuFullReductionEntity;
import person.drf.shop.coupon.entity.SkuLadderEntity;
import person.drf.shop.coupon.service.MemberPriceService;
import person.drf.shop.coupon.service.SkuFullReductionService;
import person.drf.shop.coupon.service.SkuLadderService;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Autowired
    SkuLadderService skuLadderService;

    @Autowired
    MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void saveSkuReduction(SkuReductionTo skuReductionTo) {
        // 6.4）sku的优惠、满减等信息；gulimall_sms->sms_sku_ladder\sms_sku_full_reduction\sms_member_price

        //sms_sku_ladder
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        BeanUtils.copyProperties(skuReductionTo,skuLadderEntity);
        skuLadderService.save(skuLadderEntity);

        //sms_sku_full_reduction
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(skuReductionTo,skuFullReductionEntity);
        if(skuFullReductionEntity.getFullPrice().compareTo(new BigDecimal("0"))==1){
            this.save(skuFullReductionEntity);
        }


        //sms_member_price
        List<MemberPrice> memberPrice = skuReductionTo.getMemberPrice();
        List<MemberPriceEntity> collect = memberPrice.stream().map(item -> {
            MemberPriceEntity m = new MemberPriceEntity();
            m.setSkuId(skuReductionTo.getSkuId());
            m.setMemberLevelId(item.getId());
            //m.setMemberLevelName(item.getName());
            m.setMemberPrice(item.getPrice());
            m.setAddOther(1);

            return m;
        }).filter(item->{
            return item.getMemberPrice().compareTo(new BigDecimal("0")) ==1;
        }).collect(Collectors.toList());
        memberPriceService.saveBatch(collect);
    }

}